package com.foreknow.controller;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class CookisDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建一个cookie对象 Cookie只能保存小文本信息,存储容量为4k
//        Cookie cookie = new Cookie("name","tom111");
        //设置cookie的有效时间(时间以秒为单位)
//        cookie.setMaxAge(1*30*24*60*60);
        //将cookie发送给客户端浏览器保存
//        resp.addCookie(cookie);


        //获取到客户端发送的cookie信息
        PrintWriter out = resp.getWriter();
        Cookie[] cookies = req.getCookies();
        if (cookies!=null){
            for (Cookie cc:cookies) {
                String name = cc.getName();
                String value = cc.getValue();
                out.println(name+"-----"+value);
            }
        }else{
            out.println("啥也不是,啥都没有,你找啥");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req,resp);
    }
}
